<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SysButtonController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * 列表页面
     */
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.button.list',compact('toolBarButton','menu_id'));
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if(!$params['curr_menu_id']){
            return response()->json(['code'=>400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try{
            $list = callApiServer("System.ButtonService.getInfoList", $params);
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    function showAddForm( ){
        return view('system.button.form');
    }

    function showEditForm( ){
        $params['id'] = request('button_id');
        try{
            $info = callApiServer("System.ButtonService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.button.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    function save(){
        $params['id'] = request('id', 0);
        $params['name'] = request('name');
        $params['code'] = request('code');
        $params['icon_style'] = request('icon_style');
        $params['func'] = request('func');
        $params['is_active'] = request('is_active');
        var_dump($params);

        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'按钮名称不能为空']);
        }
        if(empty($params['code'])){
            return response()->json(['code'=>400,'data'=>'code不能为空']);
        }
        if(empty($params['icon_style'])){
            return response()->json(['code'=>400,'data'=>'按钮样式不能为空']);
        }
        if(empty($params['func'])){
            return response()->json(['code'=>400,'data'=>'按钮方法名不能为空']);
        }

        $rs = callApiServer("System.ButtonService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }
}
